package com.northone.statistics;

import java.util.ArrayList;

import com.northone.statistics.fragment.HomeFragment;
import com.northone.statistics.fragment.MettingFragment;
import com.northone.statistics.fragment.SaintFragment;
import com.northone.statistics.fragment.StatisFragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class FragmentController {

	private FragmentManager manager;
	private int containerId;
	private static FragmentController controller = null;
	private ArrayList<Fragment> fragments;
	
	/** 将构造函数私有化，禁止外部创建本类对象 。 */
	private FragmentController(FragmentActivity activity, int containerId) {
		this.containerId = containerId;
		manager = activity.getSupportFragmentManager();
		initFragment();
	}
	/** 对外暴露获取本类对象的方法 。 */
	public static FragmentController getInstance(FragmentActivity activity, int containerId){
		if (controller == null) {
			synchronized (FragmentController.class) {
				if (controller == null) {
					controller = new FragmentController(activity, containerId);
				}
			}
		}
		return controller;
	}
	/** 初始化Fragment 。 */
	private void initFragment() {
		fragments = new ArrayList<Fragment>();
		fragments.add(new HomeFragment());
		fragments.add(new SaintFragment());
		fragments.add(new MettingFragment());
		fragments.add(new StatisFragment());
		
	}
	/** 替换position位置的Fragment 。 */
	public void replaceFragment(int position){
		FragmentTransaction transaction = manager.beginTransaction();
		transaction.replace(containerId, getFragment(position));
		transaction.commitAllowingStateLoss();
	}
	/*public void hideFragments() {
		*//** 隐藏所有的Fragment 。 *//*
		FragmentTransaction transaction = manager.beginTransaction();
		for (Fragment fragment : fragments) {
			if (fragment != null) {
				transaction.hide(fragment);
			}
		}
		transaction.commit();
	}*/
	
	/** 通过位置获取Fragment 。 */
	public Fragment getFragment(int position){
		return fragments.get(position);
	}
}
